﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.SYS.Store
{
    public partial class List : ListPage
    {
        BLL.V_Store bllList = new BLL.V_Store();
        BLL.Store bll = new BLL.Store();
        //
        BLL.Pic bllPic = new BLL.Pic();
        BLL.PAUser bllUser = new BLL.PAUser();
        BLL.Account_Change_Log bllAccount_Change_Log = new BLL.Account_Change_Log();
        protected bool CurrentUserInUserType = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            Master.show_btn_add = Master.show_btn_bat_del = Master.show_checkbox = Master.show_edit =
                Security.InUserType(new List<EnumHelper.UserType>() { EnumHelper.UserType.Administrator, EnumHelper.UserType.StoreServer });
            Master.show_listControls =
                Security.InUserType(new List<EnumHelper.UserType>() { EnumHelper.UserType.Administrator, EnumHelper.UserType.Accountant });
        }
        public override void buildSQL_Page_List_search()
        {
            Master.SearchSQL = "";
            if (search_text_fuzzy.Text != "")
            {
                string fuzzyText = search_text_fuzzy.Text.Replace("　", "|").Replace(" ", "|").Replace("，", "|").Replace(",", "|").Replace("||", "|");
                string fuzzySQL = "";
                foreach (string fuzzyVal in fuzzyText.Split('|'))
                {
                    if (fuzzySQL != "") fuzzySQL += " or ";
                    fuzzySQL += " name like '%" + fuzzyVal + "%' or  number like '%" + fuzzyVal + "%' or  contacts like '%" + fuzzyVal + "%' or  tel like '%" + fuzzyVal + "%' or  mobile like '%" + fuzzyVal + "%'";
                }
                Master.SearchSQL = StringPlus.SQLand(Master.SearchSQL, " ( " + fuzzySQL + ")");
            }
            //if (search_text_fuzzy.Text != "")
            //    Master.SearchSQL = StringPlus.SQLand(Master.SearchSQL, " ( name like '%" + search_text_fuzzy.Text + "%' or  number like '%" + search_text_fuzzy.Text + "%' or  contacts like '%" + search_text_fuzzy.Text + "%' or  tel like '%" + search_text_fuzzy.Text + "%' or  mobile like '%" + search_text_fuzzy.Text + "%')"); ;
            if (search_radio_state.SelectedValue != "")
                Master.SearchSQL = StringPlus.SQLand(Master.SearchSQL, "State=" + search_radio_state.SelectedValue + "");
            if (search_radio_pafeeenddate.SelectedValue != "")
            {
                string _sql_pafeeenddate="PAFeeEndDate"+(search_radio_pafeeenddate.SelectedValue=="1"?"<":">")+"getdate()";
                Master.SearchSQL = StringPlus.SQLand(Master.SearchSQL, _sql_pafeeenddate);
            }
        }
        public override DataSet get_Page_DataSet(string sql_, string orderby_, int pageFrom_, int pageTo_) { return bllList.GetListByPage(sql_, orderby_, pageFrom_, pageTo_); }
        public override void del_Page_List(string inStr_)
        {
            foreach (string id in inStr_.Split(','))
            {
                int thisID = DataTypePlus.requestInt(id);
                Model.Store model = bll.GetModel(thisID);
                int thisUserID = model.UserID;
                //
                if (DataTypePlus.requestInt(model.PicID) > 0)
                {
                    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Pic, DataTypePlus.requestInt(model.PicID), EnumHelper.OperateType.Delete, "删除店铺");
                    bllPic.Delete(DataTypePlus.requestInt(model.PicID));
                }
                foreach (Model.Account_Change_Log log in bllAccount_Change_Log.GetModelList("StoreID=" + thisID))
                {
                    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Account_Change_Log, log.id, EnumHelper.OperateType.Delete, "删除店铺");
                    bllAccount_Change_Log.Delete(log.id);
                }
                //
                bllUser.Delete(thisUserID);
                bll.Delete(thisID);
                //
                BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Store, thisID, EnumHelper.OperateType.Delete, "");
                BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.PAUser, thisUserID, EnumHelper.OperateType.Delete, "");
            }
        }

    }
}